<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite pt20 pb10 radius10 pl10 pr20 justify-between">
      <div>
        <el-input v-model="uid" style="width: 280px" placeholder="搜尋用戶ID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-select v-model="income_type" placeholder="選擇收支類型" size="large" style="width: 240px" class="mb10">

          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>

      </div>
      <div class="noshrink mb10">
        <el-button class="btn2" @click="getBackChargeList()">查詢</el-button>
        <el-button class="btn1" @click="reset()">重置</el-button>
      </div>
    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="tableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="id" label="序號" sortable width="100" />
          <el-table-column prop="user" label="用戶訊息" width="200">
            <template #default="scope">
              <span @click="showUser(scope.row.user.id)">{{ scope.row.user.phone }}(ID:{{ scope.row.user.id }})</span>

            </template>
          </el-table-column>
          <el-table-column prop="coin" label="金幣" min-width="100" />
          <el-table-column prop="income_type_text" label="收支類型" min-width="100" />
          <el-table-column prop="before_num" label="變更之前" min-width="100" />
          <el-table-column prop="after_num" label="變更之後" min-width="100" />


          <el-table-column prop="created_at" label="創建時間" min-width="150" />
          <el-table-column prop="updated_at" label="更新時間" sortable min-width="150" />
          <el-table-column prop="" label="操作" width="150" fixed="right">
            <template #default="scope">
              <span class=" pointer" @click="dialogShow(scope.row)">管理員操作記錄</span>


            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="mlauto plr20 mt15">
        <el-pagination v-model:current-page="currentPage" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :background="true" layout="prev, pager, next, jumper" :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
      </div>
    </div>
    <el-dialog v-model="dialogTableVisible" title="管理員操作記錄" width="900">

      <el-table :data="filterTableData" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
        <el-table-column property="adminuser.username" label="管理員訊息" />
        <el-table-column property="created_at" label="操作時間" />

      </el-table>
      <div class="  plr20 justify-between mt20">
        <div class="noshrink">總共{{ filterTotal }}條</div>
        <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :background="true" layout="prev, pager, next, jumper" :total="filterTotal" @size-change="handleSizeChange" @current-change="handleCurrentChange2" />
      </div>
    </el-dialog>

  </div>
</template>

<script setup lang="ts">
import { Search } from '@element-plus/icons-vue'
import { ref, onMounted } from 'vue'
import { ElTable } from 'element-plus'
import { inject } from 'vue';
const showUser = inject('showUser');
interface User {
  date: string
  name: string
  address: string
}
import { backChargeList } from '@/api/finance.js'
onMounted(() => {
  getBackChargeList()
})

// 后台充值记录
const getBackChargeList = () => {
  backChargeList({
    page: currentPage.value,
    uid: uid.value,
    income_type: income_type.value
  }).then((res) => {
    console.log('后台充值记录', res)
    tableData.value = res.data.data
    total.value = res.data.total
  })
}
const reset = () => {
  uid.value = ''
  income_type.value = ''
  getBackChargeList()
}
const uid = ref('')
const income_type = ref('')

const dialogTableVisible = ref(false)


const tableData = ref([])
const currentPage = ref(1)
const total = ref(0)
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  getBackChargeList()
}
const options = [
  {
    label: '增加',
    value: '1'
  },
  {
    label: '減少',
    value: '2'
  }
]
const currentUid = ref('')
const filterTableData = ref([])
const currentPage2 = ref(1)
const filterTotal = ref(0)
const handleCurrentChange2 = (val) => {
  console.log(`current page: ${val}`)
  getAdminRecord(currentUid.value)
}

const dialogShow = (row) => {
  dialogTableVisible.value = true
  currentUid.value = row.user_id
  getAdminRecord(row.user_id)
}
const getAdminRecord = (uid) => {
  backChargeList({
    page: currentPage2.value,
    uid: uid,
  }).then((res) => {
    console.log('后台充值记录', res)
    filterTableData.value = res.data.data
    filterTotal.value = res.data.total
  })
}
</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}
</style>
<style></style>
